<?php
declare (strict_types=1);

namespace app\business\controller;

use app\common\model\Area;
use app\common\model\ProductCate;
use think\App;
use think\facade\View;
use think\response\Json;

class Business extends Base
{

    //主键
    protected $pk = 'id';

    //主模型
    protected $model = '\app\common\model\Business';


    public function edit($name = '', $id = '', $tpl = 'business/edit', $info = '')
    {
        $info = $this->business;
        $info['license'] = $info['license'] ? explode(',', $info['license']) : '';

        View::assign('bank', \app\common\model\Bank::select());
        View::assign('cate', ProductCate::getCate());
        return parent::edit($name, $id, $tpl, $info); // TODO: Change the autogenerated stub
    }

    public function save($param = [], $model = false): Json
    {
        $param = $this->request->post();
        $param['license'] = isset($param['license']) ? implode(',', $param['license']) : [];
        $address = Area::where('code', $param['district_id'])->find();
        $return_address = Area::where('code', $param['return_district_id'])->find();
        if (empty($address)) {
            return json(['code' => -1, 'msg' => '请设置地址！']);
        }
        $param['full_address'] = $address->province . $address->city . $address->district . $param['address_details'];
        $param['return_full_address'] = $return_address->province . $return_address->city . $return_address->district . $param['return_address_details'];
        return parent::save($param, $model); // TODO: Change the autogenerated stub
    }
}
